module bookmark_data

entity Bookmark {
	url		::	URL (id)
	bmentries	->	List<BookmarkEntry>
	date		::	Date
	popularity	::	Int
	
	function matches(filt : String) : Bool {
	  for(bmentry: BookmarkEntry in this.bmentries) {
	    if(bmentry.matches(filt)) {
	      return true;
	    }
	  }
	  return false;
	}
	
	function bmentries_fromuser(u : User) : BookmarkEntry {
	  for(bmentry : BookmarkEntry in this.bmentries) {
	    if(bmentry.user == u) {
	      return bmentry;
	    }
	  }
	  return null;
	}
	
	function bmentries_add(bmentry: BookmarkEntry) {
	  this.bmentries.add(bmentry);
	  this.updatepopularity();
	}
	
	function bmentries_remove(bmentry: BookmarkEntry) {
	  this.bmentries.remove(bmentry);
	  this.updatepopularity();
	}
	
	function updatepopularity() {
	  this.popularity := this.bmentries.length;
	  for(bmentry: BookmarkEntry in this.bmentries) {
	    bmentry.user.bmentries_setpopularity(bmentry, this.popularity);
	  }
	}
}
